// 添加专利及著作权

layui.use(['form', 'code', 'laydate','upload','layer','laytpl','element'], function() {
	var laydate = layui.laydate;
	form = layui.form;
	upload = layui.upload;
	layer = layui.layer;
	laytpl = layui.laytpl;
	element = layui.element;


	layui.code();

	// 时间选择
	laydate.render({
		elem: '.awardDate' //指定元素
	});


	$("#submitNewAward").click(function () {
		// 判断必填项--提示--统校验
		var params = {};

		// 集合所有参数
		$("#addNewAwardForm .save_val").each(function() {
			var name = $(this).attr("name");
			var val = $(this).val();
			params[name] = val;
		});

		if(!params.projectName){
			layer.msg("项目名称不能为空");
			return false;
		}
		if(!params.awardName){
			layer.msg("奖励名称不能为空");
			return false;
		}
		if(!params.grade){
			layer.msg("等级不能为空");
			return false;
		}
		if(!params.awardDate){
			layer.msg("获奖时间不能为空");
			return false;
		}
		if(!params.awardedOrgan){
			layer.msg("授权机构不能为空");
			return false;
		}


		var url = cgiMain.academistAwardAdd;
		if(params.id && params.id > 0){
			url = cgiMain.academistAwardEdit;
		}


		AJAX.post(url,
			params,
			function (res) {
				if(res.code=='0000'){
					layer.msg('保存成功');
					$("#myModal").hide();
					window.location.reload();
				}
			});
		return false;
	});



	function  setSelect(dom,data){
		for(let item in data){
			$(dom).append("<option value='" + item +"'>"+ data[item] +"</option>")
		}
		form.render("select");
	}



// 添加教育经验
	$(".add_btn").on("click", function() {
		$("#myModal").show();
		$("#myModal input, #myModal textarea").val("");
	});




	//初始化列表
	setTimeout(function () {
		AJAX.post(cgiMain.academistAwardList,
			{},
			function (res) {

				if(res.code == '0000'){
					$(".awardList").data("awardList",res.result);
					var tpl = $("#awardItemTpl").html();
					for(var i = 0; i < res.result.length;i++){
						laytpl(tpl).render(res.result[i],function (html) {
							$(".awardList").append(html);
							$(".awardList").on("click",'.editAward',function (elem) {
								editAward(elem);

							});
							$(".awardList").on("click",'.deleteAward',function (elem) {
								deleteAward(elem);
							});
						});
					}
					element.render(".awardList");
				}

			});

	},500);


	function editAward(dom){

		var awardId = $(dom.target).attr("data-id");
		var awardList = $(".awardList").data("awardList");

		var academistAward={};
		for(var i = 0 ; i < awardList.length;i++){
			if(awardList[i].id == awardId){
				academistAward = awardList[i];
				break;
			}
		}

		$("input[name='id']").val(academistAward.id);
		$(".awardDate").val(academistAward.awardDate);
		$(".projectName").val(academistAward.projectName);
		$(".awardName").val(academistAward.awardName);
		$(".grade").val(academistAward.grade);
		$(".awardedOrgan").val(academistAward.awardedOrgan);

		$("#myModal").show();
	}

	function deleteAward(dom){

		var awardId = $(dom.target).attr("data-id");

		layer.open({
			content: '确定删除吗?'
			,btn: ['删除', '取消']
			,yes: function(index, layero){
				//按钮【按钮一】的回调
				AJAX.post(cgiMain.academistAwardDelete,
					{id:awardId},
					function (res) {
						if(res.code=='0000'){
							layer.msg('删除成功');
							window.location.reload();
						}
					});


			}
			,btn2: function(index, layero){
				//按钮【按钮二】的回调
			}

			,cancel: function(){
				//右上角关闭回调
			}
		});


	}

	$(".modal-footer > .layui-btn-primary").on("click",function () {
		$("#myModal").hide();
		$("#myModal input, #myModal textarea").val("");
	});



});



